﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Application.Project;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Web;
namespace Logiv.WEB.Areas.Project.Controllers
{
    public class SalaryController : BaseController
    {
        SalaryApp salaryService = new SalaryApp();
        UserApp useService = new UserApp();
        DutyApp dutyService = new DutyApp();
        private PersonSalaryApp perService = new PersonSalaryApp();
        private PositionRecordApp posService = new PositionRecordApp();
        #region 运量统计

        public ActionResult YunLiangIndex()
        {
            return View();
        }

        public ActionResult YunLiangDetails()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonYunLiang(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = salaryService.GetSalary(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  NeedToPay = num.NeedToPay,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  Persion_F_Id = num.Persion_F_Id,
                                  Worker = useService.GetForm(num.Persion_F_Id),
                                  Duty = dutyService.GetForm(useService.GetForm(num.Persion_F_Id).F_DutyId)
                              }).ToList().OrderBy(f => f.Duty.F_FullName);

                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public ActionResult GetGridJsonYunLiangDetailsExportExcell(string keyword, string StartTime, string EndTime)
        {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            try
            {
                var reuslt = perService.GetYunLiangList(pagination2, keyword, StartTime, EndTime);
                if (reuslt != null && reuslt.Count > 0)
                {
                    List<YunLiangTongjiObj> exportdata = new List<YunLiangTongjiObj>();
                    foreach (var yunLiangTongjiObj in reuslt)
                    {
                        YunLiangTongjiObj temp = new YunLiangTongjiObj(yunLiangTongjiObj);
                        exportdata.Add(temp);
                    }
                    Type entityType = exportdata[0].GetType();
                    PropertyInfo[] entityProperties = entityType.GetProperties();
                    ArrayList list = new ArrayList();
                    for (int i = 0; i < entityProperties.Length; i++)
                    {
                        list.Add(entityProperties[i].Name);
                    }
                    string[] arrString = (string[])list.ToArray(typeof(string));
                    AsposeExcel.SaveFile(AsposeExcel.ListToDataTable(exportdata), "单人运量统计数据.xls", arrString);
                    return null;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonYunLiangDetails(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = perService.GetYunLiangList(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  F_Id = num.F_Id,
                                  EveryTimeSalary = num.EveryTimeSalary,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  F_CreatorUserId = num.F_CreatorUserId,
                                  F_CreatorTime = num.F_CreatorTime,
                                  EveryCarID = num.EveryCarID == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.EveryCarID.F_Id,
                                      CarriageID = num.EveryCarID.CarriageID,
                                      ThingsName = num.EveryCarID.ThingsName,
                                      CarType = num.EveryCarID.CarType,
                                      Customers = num.EveryCarID.Customers,
                                      ArriveTime = num.EveryCarID.ArriveTime,
                                      ArriveStation= num.EveryCarID.ArriveStation
                                  },
                                  OperationGudaoCarEntity = num.OperationGudaoCarEntity == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.OperationGudaoCarEntity.F_Id,
                                      IsRollBack = num.OperationGudaoCarEntity.IsRollBack,
                                      RollbackTime = num.OperationGudaoCarEntity.RollbackTime,
                                      F_CreatorUserId = num.OperationGudaoCarEntity.F_CreatorUserId,
                                      OperationPersion = num.OperationGudaoCarEntity.OperationPersion == null ? null : new UserEntity()
                                      {
                                          F_Id = num.OperationGudaoCarEntity.OperationPersion.F_Id,
                                          F_RealName = num.OperationGudaoCarEntity.OperationPersion.F_RealName
                                          //F_RealName = useService.GetForm(num.OperationGudaoCarEntity.OperationPersion.F_Id).F_RealName
                                      }
                                  },
                                  Remake = num.Remake,
                                  Banci = posService.GetForm(f => f.F_Id == num.OperationGudaoCarEntity.Banci),
                                  Leader = useService.GetForm(num.OperationGudaoCarEntity.F_CreatorUserId).F_RealName
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion


        #region 工资汇总

        public ActionResult TotalSalaryIndex()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonTotal(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = salaryService.GetTotalSalary(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  NeedToPay = num.NeedToPay,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  Persion_F_Id = num.Persion_F_Id,
                                  Worker = useService.GetForm(num.Persion_F_Id),
                                  Duty = dutyService.GetForm(useService.GetForm(num.Persion_F_Id).F_DutyId)
                              }).ToList().OrderBy(f => f.Duty.F_FullName);
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion


        #region 放行统计

        public ActionResult FangXingIndex()
        {
            return View();
        }

        public ActionResult FangXingDetails()
        {
            return View();
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonFangXing(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = salaryService.GetFangXingSalary(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  NeedToPay = num.NeedToPay,
                                  //Weight = num.Weight,
                                  //DWeight = num.DWeight,
                                  Persion_F_Id = num.Persion_F_Id,
                                  TotalCarNum = num.TotalCarNum,
                                  Worker = useService.GetForm(num.Persion_F_Id),
                                  Duty = dutyService.GetForm(useService.GetForm(num.Persion_F_Id).F_DutyId)
                              }).ToList().OrderBy(f => f.Duty.F_FullName);
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public ActionResult GetGridJsonFangXingDetailsExportExcell(string keyword, string StartTime, string EndTime)
        {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            try
            {
                var reuslt = perService.GetFangXingList(pagination2, keyword, StartTime, EndTime);
                if (reuslt != null && reuslt.Count > 0)
                {
                    List<YunLiangTongjiObj> exportdata = new List<YunLiangTongjiObj>();
                    foreach (var yunLiangTongjiObj in reuslt)
                    {
                        YunLiangTongjiObj temp = new YunLiangTongjiObj(yunLiangTongjiObj);
                        exportdata.Add(temp);
                    }
                    Type entityType = exportdata[0].GetType();
                    PropertyInfo[] entityProperties = entityType.GetProperties();
                    ArrayList list = new ArrayList();
                    for (int i = 0; i < entityProperties.Length; i++)
                    {
                        list.Add(entityProperties[i].Name);
                    }
                    string[] arrString = (string[])list.ToArray(typeof(string));
                    AsposeExcel.SaveFile(AsposeExcel.ListToDataTable(exportdata), "单人放行统计数据.xls", arrString);
                    return null;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonFangXingDetails(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = perService.GetFangXingList(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  F_Id = num.F_Id,
                                  EveryTimeSalary = num.EveryTimeSalary,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  F_CreatorUserId = num.F_CreatorUserId,
                                  F_CreatorTime = num.F_CreatorTime,
                                  EveryCarID = num.EveryCarID == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.EveryCarID.F_Id,
                                      CarriageID = num.EveryCarID.CarriageID,
                                      ThingsName = num.EveryCarID.ThingsName,
                                      CarType = num.EveryCarID.CarType,
                                      Customers = num.EveryCarID.Customers,
                                      ArriveTime = num.EveryCarID.ArriveTime,
                                      ArriveStation = num.EveryCarID.ArriveStation
                                  },
                                  OperationGudaoCarEntity = num.OperationGudaoCarEntity == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.OperationGudaoCarEntity.F_Id,
                                      IsRollBack = num.OperationGudaoCarEntity.IsRollBack,
                                      RollbackTime = num.OperationGudaoCarEntity.RollbackTime,
                                      F_CreatorUserId = num.OperationGudaoCarEntity.F_CreatorUserId,
                                      OperationPersion = num.OperationGudaoCarEntity.OperationPersion == null ? null : new UserEntity()
                                      {
                                          F_Id = num.OperationGudaoCarEntity.OperationPersion.F_Id,
                                          F_RealName = num.OperationGudaoCarEntity.OperationPersion.F_RealName
                                          //F_RealName = useService.GetForm(num.OperationGudaoCarEntity.OperationPersion.F_Id).F_RealName
                                      }
                                  },
                                  Remake = num.Remake,
                                  Banci = posService.GetForm(f => f.F_Id == num.OperationGudaoCarEntity.Banci),
                                  Leader = useService.GetForm(num.OperationGudaoCarEntity == null ? "" : num.OperationGudaoCarEntity.F_CreatorUserId)?.F_RealName
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion

        #region 对位

        public ActionResult DuiweiIndex()
        {
            return View();
        }
        public ActionResult DuiweiDetails()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonDuiwei(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = salaryService.GetSalaryDuiwei(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  NeedToPay = num.NeedToPay,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  Persion_F_Id = num.Persion_F_Id,
                                  Worker = useService.GetForm(num.Persion_F_Id),
                                  Duty = dutyService.GetForm(useService.GetForm(num.Persion_F_Id).F_DutyId)
                              }).ToList().OrderBy(f => f.Duty.F_FullName);
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonDuiweiDetails(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            try
            {
                var reuslt = perService.GetDuiweiList(pagination, keyword, StartTime, EndTime);
                var result = (from num in reuslt
                              select new
                              {
                                  F_Id = num.F_Id,
                                  EveryTimeSalary = num.EveryTimeSalary,
                                  Weight = num.Weight,
                                  DWeight = num.DWeight,
                                  F_CreatorUserId = num.F_CreatorUserId,
                                  F_CreatorTime = num.F_CreatorTime,
                                  EveryCarID = num.EveryCarID == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.EveryCarID.F_Id,
                                      CarriageID = num.EveryCarID.CarriageID

                                  },
                                  OperationGudaoCarEntity = num.OperationGudaoCarEntity == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.OperationGudaoCarEntity.F_Id,
                                      IsRollBack = num.OperationGudaoCarEntity.IsRollBack,
                                      RollbackTime = num.OperationGudaoCarEntity.RollbackTime,
                                      F_CreatorUserId = num.OperationGudaoCarEntity.F_CreatorUserId,
                                      OperationPersion = num.OperationGudaoCarEntity.OperationPersion == null ? null : new UserEntity()
                                      {
                                          F_Id = num.OperationGudaoCarEntity.OperationPersion.F_Id,
                                          F_RealName = num.OperationGudaoCarEntity.OperationPersion.F_RealName
                                          //F_RealName = useService.GetForm(num.OperationGudaoCarEntity.OperationPersion.F_Id).F_RealName
                                      }
                                  },
                                  Remake = num.Remake,
                                  Banci = posService.GetForm(f => f.F_Id == num.OperationGudaoCarEntity.Banci),
                                  Leader = useService.GetForm(num.OperationGudaoCarEntity.F_CreatorUserId).F_RealName
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion



    }
}